package org.example.rmi;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.rmi.RmiRegistryFactoryBean;
import org.springframework.remoting.rmi.RmiServiceExporter;

import javax.net.ssl.SSLContext;
import javax.rmi.ssl.SslRMIServerSocketFactory;
import java.io.IOException;
import java.rmi.server.RMIServerSocketFactory;

@Configuration
public class RmiServer {

    @Bean
    public Hello hello() {
        return new HelloImpl();
    }

    @Bean
    public Hello hello2() {
        return () -> System.out.println("hello2");
    }

    @Bean
    public RmiServiceExporter rmiServiceExporter(RmiRegistryFactoryBean rmiRegistryFactoryBean) throws Exception {
        RmiServiceExporter rmiServiceExporter = new RmiServiceExporter();
        rmiServiceExporter.setServiceName("hello");
        rmiServiceExporter.setService(hello());
        rmiServiceExporter.setServiceInterface(Hello.class);
//        rmiServiceExporter.setRegistryPort(8848);
//        rmiServiceExporter.setClientSocketFactory(new MyRMIClientSocketFactory());
//        rmiServiceExporter.setServerSocketFactory(getRmiServerSocketFactory());
//        rmiServiceExporter.setRegistryClientSocketFactory(new MyRMIClientSocketFactory());
//        rmiServiceExporter.setRegistryServerSocketFactory(getRmiServerSocketFactory());
        rmiServiceExporter.setRegistry(rmiRegistryFactoryBean.getObject());
        return rmiServiceExporter;
    }

    @Bean
    public RmiServiceExporter rmiServiceExporter2(RmiRegistryFactoryBean rmiRegistryFactoryBean) throws Exception {
        RmiServiceExporter rmiServiceExporter = new RmiServiceExporter();
        rmiServiceExporter.setServiceName("hello2");
        rmiServiceExporter.setService(hello2());
        rmiServiceExporter.setServiceInterface(Hello.class);
//        rmiServiceExporter.setRegistryPort(8848);
//        rmiServiceExporter.setClientSocketFactory(new MyRMIClientSocketFactory());
//        rmiServiceExporter.setServerSocketFactory(getRmiServerSocketFactory());
//        rmiServiceExporter.setRegistryClientSocketFactory(new MyRMIClientSocketFactory());
//        rmiServiceExporter.setRegistryServerSocketFactory(getRmiServerSocketFactory());
        rmiServiceExporter.setRegistry(rmiRegistryFactoryBean.getObject());
        return rmiServiceExporter;
    }

    @Bean
    public RmiRegistryFactoryBean rmiRegistryFactoryBean() throws Exception {
        RmiRegistryFactoryBean rmiRegistryFactoryBean = new RmiRegistryFactoryBean();
        rmiRegistryFactoryBean.setPort(8848);
        rmiRegistryFactoryBean.setClientSocketFactory(new MyRMIClientSocketFactory());
        rmiRegistryFactoryBean.setServerSocketFactory(getRmiServerSocketFactory());
        return rmiRegistryFactoryBean;
    }

    public static RMIServerSocketFactory getRmiServerSocketFactory() throws Exception {
        SSLContext sslContext = MyRMIClientSocketFactory.getSSLContext();
        return new SslRMIServerSocketFactory(sslContext, null, new String[]{"TLSv1.2"}, true);
    }

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(RmiServer.class);
//        ServerSocket serverSocket = new ServerSocket(8848);
//        System.out.println("success");
    }
}
